package ModelImplementation

import (
	"salaryPayment/Model"
	"math"
	"time"
)

type HourlyClassification struct {
	itsRate      float64
	itsTimeCards map[time.Time]TimeCard
}

func (hc *HourlyClassification) GetRate() float64 {
	return hc.itsRate
}

func (hc *HourlyClassification) SetRate(hourlyRate float64) {
	hc.itsRate = hourlyRate
}


func (hc *HourlyClassification) GetTimeCard(date time.Time) TimeCard {
	return hc.itsTimeCards[date]
}

func (hc *HourlyClassification) AddTimeCard(tc TimeCard) {
	hc.itsTimeCards[tc.GetDate()] = tc
}

func (hc *HourlyClassification) calculatePayForTimeCard(tc TimeCard) float64 {
	hours := tc.GetHours()
	overtime := math.Max(0, hours-8)
	straightTime := hours - overtime
	cpk :=straightTime*hc.itsRate + overtime*hc.itsRate*1.5
	return cpk
}

func (hc *HourlyClassification) CalculatePay(pc Model.Paycheck) float64 {
	var totalPay float64

	for date := range hc.itsTimeCards {
		tc := hc.itsTimeCards[date]

		dt1 := pc.GetPayPeriodStartDate()
		dt2 := pc.GetPayPeriodEndDate()

		if date.After(dt1) && date.Before(dt2) {
			totalPay += hc.calculatePayForTimeCard(tc)
		}
	}

	return totalPay
}
